
exports.get = function(player, req, resp, onHandled) {

    if( player.user.status.level < 20 ) {
        resp.code = 1; resp.desc = 'level not reach' ;
        onHandled();
        return;
    }
    
    requestWorld(req, resp, function(){
        if( 0 == resp.code ) {
            var league = player.user.league;
            if( resp.data.league ) {
                var tech = resp.data.league.tech;
                var userTech = league.tech || {};
                var techChange = false;
                for( var id in tech ) {
                    if( !(id in userTech) || tech[id] != userTech[id] ) {
                        techChange = true;
                        break;
                    } 
                }
                
                if( techChange ) {
                    player.roleDirty = true;
                    league.tech = tech;
                    player.markDirty('league.tech'); 
                }

                if( league.name != resp.data.league.name ) {
                    league.name = resp.data.league.name;
                    player.markDirty('league.name');
                }
            }else{
                if( !req.args.list ) {
                    if( Object.keys(league.tech).length > 0 ) {
                        league.tech = {};
                        player.roleDirty = true;
                        player.markDirty('league.tech'); 
                    }

                    if( league.name ) {
                        league.name = '';
                        player.markDirty('league.name');
                    }
                }
            }
        }

        onHandled();
    });
}

exports.create = function(player, req, resp, onHandled) {
    var league = player.user.league;
    var costCash = gConfGlobal.CreateLeagueCash;
    do {
        if( player.user.status.level < 20 ) {
            resp.code = 1; resp.desc = 'level not reach'; break;
        }

        if( ['wei', 'wu', 'shu'].indexOf(req.args.country) < 0 ) {
            resp.code = 1; resp.desc = 'invalid country'; break;
        }

        if( !verifyWordFilter(req.args.name, req.args.key) ) {
            resp.code = 1; resp.desc = 'verify fail'; break;
        }

        var name = req.args.name.toString().trim();
        if( name.length == 0 || name == '0' || name.length > 5 
            || name.indexOf('.') >= 0 || name.indexOf('$') >= 0) {
            resp.code = 1; resp.desc = 'invalid name'; break;
        }
        req.args.name = name;

        if( player.user.status.cash < costCash ) {
            resp.code = 1; resp.desc = 'not enough cash'; break;
        }

    } while (false);

    if( 1 == resp.code ) {
        onHandled(); 
        return;
    }

    requestWorld(req, resp, function(){
        if( 0 == resp.code ) {
            if( !resp.data.update && !resp.data.exited ) {
                player.costResource('cash', costCash);
                resp.data.cash = -costCash;
            }
        }
        onHandled();
    });

}

exports.get_applying = function(player, req, resp, onHandled) {
    requestWorld(req, resp, onHandled);
}

exports.apply = function(player, req, resp, onHandled) {

    if( player.user.status.level < 20 ) {
        resp.code = 1; resp.desc = 'level not reach'; 
        onHandled();
        return;
    }
    
    requestWorld(req, resp, onHandled);
}

exports.cancel_apply = function(player, req, resp, onHandled) {

    requestWorld(req, resp, onHandled);
}

exports.handle_apply = function(player, req, resp, onHandled) {
    requestWorld(req, resp, onHandled);
}

exports.set_notice = function(player, req, resp, onHandled) {
    do {
        var content = req.args.introduce + 'f26b59ydq' + req.args.notice;
        if( !verifyWordFilter(content, req.args.key) ) {
            resp.code = 1; resp.desc = 'verify fail'; break;
        }
    } while(false);

    if( 1 == resp.code ) {
        onHandled();
        return;
    }

    requestWorld(req, resp, onHandled);
}

exports.promote = function(player, req, resp, onHandled) {

    requestWorld(req, resp, onHandled);
}

exports.dismiss = function(player, req, resp, onHandled) {

    requestWorld(req, resp, onHandled);
}

exports.fire = function(player, req, resp, onHandled) {

    requestWorld(req, resp, onHandled);
}

exports.transfer = function(player, req, resp, onHandled) {

    requestWorld(req, resp, onHandled);
}

exports.impeach = function(player, req, resp, onHandled) {
    var cashCost = gConfGlobal.CreateLeagueCash;
    do {
        if( player.user.status.cash < cashCost ) {
            resp.code = 1; resp.desc = 'not enough cash'; break;
        }
    } while(false);

    if( 1 == resp.code ) {
        onHandled();
        return;
    }

    requestWorld(req, resp, function(){
        if( 0 == resp.code && !resp.data.update ) {
            player.costResource('cash', cashCost);
            resp.data.cash = -cashCost;
        }

        onHandled();
    });
}

exports.search = function(player, req, resp, onHandled) {

    requestWorld(req, resp, onHandled);
}

exports.view_league = function(player, req, resp, onHandled) {

    requestWorld(req, resp, onHandled);
}

exports.exit = function(player, req, resp, onHandled) {
    requestWorld(req, resp, function(){
        if( (0 == resp.code) && (!resp.data.update) ) {
            player.user.league.tech = {};

            player.roleDirty = true;
            player.markDirty('league.tech');
        }

        onHandled();
    });
}

exports.contribute = function(player, req, resp, onHandled) {

    if( isNaN(req.args.cash) || Math.round(req.args.cash) < 1 || 
            player.user.status.cash < Math.round(req.args.cash) ) {

        resp.code = 1; resp.desc = 'not enough cash';  
        onHandled();
        return;
    }

    var cashCost = Math.round(req.args.cash);
    requestWorld(req, resp, function(){

        if( 0 == resp.code && !resp.data.update ) {
            player.costResource('cash', cashCost);
            player.user.league.contribute += resp.data.contribute; 
            player.markDirty('league.contribute');
            resp.data.cash = -cashCost;
        }

        onHandled();
    });
}

exports.upgrade_tech = function(player, req, resp, onHandled) {

    requestWorld(req, resp, function(){

        if( 0 == resp.code && !resp.data.update ) {
            var tech = player.user.league.tech;
            var id = +req.args.id;
            var newLevel = 1;
            if( tech[id] ) {
                newLevel = tech[id] + 1; 
            }

            tech[id] = newLevel;
            player.markDirty('league.tech');
            player.roleDirty = true;
        }

        onHandled();
    });
}

exports.get_reward = function(player, req, resp, onHandled) {
    do{
        var user = player.user;

        var opened = user.league.opened;
        var sum = 0;
        for( var pos in opened ) {
           sum += +opened[pos]; 
        }

        var leagueRewardConf = gConfLeagueReward[sum];
        if( !leagueRewardConf ) {
            resp.code = 1; resp.desc = 'num error'; break;
        }


        user.league.opened = {};
        var status = player.user.status;     
        status.gold += leagueRewardConf.Gold;
        status.honor += leagueRewardConf.Honor;
        user.mark.league += 1;

        player.markDirty('status');
        player.markDirty('mark.league');
        player.markDirty('league.opened');

        resp.data.gold = leagueRewardConf.Gold;
        resp.data.honor = leagueRewardConf.Honor;

        var awards = [leagueRewardConf.Award1];
        awards = player.addAwards(awards);
        resp.data.awards = awards;
    }while(false);

    onHandled();
}

exports.open = function(player, req, resp, onHandled) {
    do {
        if( isNaN(req.args.pos) ) {
            resp.code = 1; resp.desc = 'no valid args'; break;
        }

        var pos = Math.floor(+req.args.pos);
        if( pos < 1 || pos > 9 ) {
            resp.code = 1; resp.desc = 'pos error' ; break;
        }
        
        var user = player.user;
        if( user.mark.league >= gConfGlobal.LeagueGameCountPerDay ) {
            resp.code = 1; resp.desc = 'no times'; break;
        }

        if( user.league.contribute < gConfGlobal.LeagueContributePerCard ) {
            resp.code = 1; resp.desc = 'not enough contribute'; break;
        }

        var userLeagueOpened = user.league.opened;
        if( Object.keys(userLeagueOpened).length >= 3 ) {
            resp.code = 1; resp.desc = 'fuck'; break;
        }

        var selects = {'1':1, '2':1, '3':1, '4':1, '5':1, '6':1, '7':1, '8':1, '9':1};
        for( var openPos in userLeagueOpened ) { 
            var num = userLeagueOpened[openPos]; 
            if( num in selects ) {
                delete selects[num];
            }
        }
        
        var num = +common.wRand(selects);
        resp.data.num = num;

        user.league.opened[pos] = num;
        user.league.contribute -= gConfGlobal.LeagueContributePerCard ;

        player.markDirty('league.contribute');
        player.markDirty('league.opened');
    }while(false);

    onHandled();
}
